package my.soft.aiteacher.teacher.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import my.soft.aiteacher.exception.BusinessException;
import my.soft.aiteacher.teacher.mapStruct.SmartAnalysisMapStruct;
import my.soft.aiteacher.teacher.mapper.FileUploadMapper;
import my.soft.aiteacher.teacher.mapper.SmartAnalysisMapper;
import my.soft.aiteacher.teacher.pojo.FileUpload;
import my.soft.aiteacher.teacher.pojo.SmartAnalysisDo;
import my.soft.aiteacher.teacher.pojo.SmartAnalysisVo;
import my.soft.aiteacher.teacher.service.ISmartAnalysisService;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class SmartAnalysisServiceImpl extends ServiceImpl<SmartAnalysisMapper, SmartAnalysisDo> implements ISmartAnalysisService {
   @Resource
   private SmartAnalysisMapper smartAnalysisMapper;
   @Resource
   private SmartAnalysisMapStruct smartAnalysisMapStruct;
   @Resource
   private FileUploadMapper fileUploadMapper;
    @Override
    public void upload(SmartAnalysisVo smartAnalysisVo) {
        smartAnalysisMapper.delete(new QueryWrapper<SmartAnalysisDo>().eq("file_id",smartAnalysisVo.getFileId()));
        if (Objects.isNull(smartAnalysisVo.getFileId())){
            throw new BusinessException("未携带文件id,请联系管理员");
        }
        FileUpload fileUpload = fileUploadMapper.selectById(smartAnalysisVo.getFileId());
        if (Objects.isNull(fileUpload)){
            throw new BusinessException("缺少文件信息，请联系管理员");
        }
        SmartAnalysisDo smartAnalysisDo = smartAnalysisMapStruct.toSmartAnalysisDo(smartAnalysisVo);
        smartAnalysisMapper.insert(smartAnalysisDo);
    }

    @Override
    public SmartAnalysisVo Detail(Long fileId) {
        SmartAnalysisDo Do = smartAnalysisMapper.selectOne(new QueryWrapper<SmartAnalysisDo>().eq("file_id", fileId));
        if (Objects.isNull(Do)){
            throw new BusinessException("不存在该内容，请联系管理员");
        }
        return smartAnalysisMapStruct.toSmartAnalysisVo(Do);
    }
}
